package com.anyisn.anyflickr.shell.view.components
{
	import com.adobe.webapis.flickr.PagedPhotoList;
	import com.adobe.webapis.flickr.Photo;
	import com.anyisn.anyflickr.shell.view.components.itemrender.ThumbIR;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Tile;
	
	public class ThumbsTile extends Tile
	{
		public static const MENU_SHOW:String = 'menuShow';
		public var selectedItem:Photo;
		private var _oldThumbsList:PagedPhotoList;
		private var _thumbsList:PagedPhotoList;
		
		public function ThumbsTile()
		{
			super();
			this.direction = "vertical";
			this.horizontalScrollPolicy = "auto";
			this.verticalScrollPolicy = "auto";
			_thumbsList = new PagedPhotoList();
		}
		
		public function initialThumbs(list:PagedPhotoList):void {
			_oldThumbsList = _thumbsList;
			_thumbsList = list;
		}
		
		public function clear():void {
			for(var i:int=0;i<_oldThumbsList.photos.length;i++) {
				var thumb:ThumbIR = getChildAt(i) as ThumbIR;
				thumb.removeEventListener(MouseEvent.RIGHT_CLICK, showMenu);
			}
		}
		
		public function addThumbs():void {
			for(var i:int=0;i<_thumbsList.photos.length;i++) {
				var thumb:ThumbIR = new ThumbIR(_thumbsList.photos[i]);
				thumb.addEventListener(MouseEvent.RIGHT_CLICK, showMenu);
				addChild(thumb);
			}
		}
		
		private function showMenu(e:MouseEvent):void {
			selectedItem = e.currentTarget.photo;
			dispatchEvent(new Event(MENU_SHOW));
		}
	}
}